﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class AdminRoleGateway:DBGateway
    {
        public string SaveRole(AdminRole adminRole)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =string.Format("insert into tbAdminRole values(@RoleName)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@RoleName", SqlDbType.VarChar).Value =adminRole.RoleName;
                CommandObj.ExecuteNonQuery();
                return "Admin role save successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }
        public string UpdateRole(AdminRole adminRole)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Update tbAdminRole Set RoleName='{0} where Id='{1}'",adminRole.Id);
                CommandObj.ExecuteNonQuery();
                return "Admin role update successfully";
            }
            catch (Exception)
            {
                throw new Exception("Role update failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }

        public bool IsThisRoleExist(AdminRole adminRole)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Select *  from tbAdminRole where RoleName='{0} or Id='{1}'",adminRole.RoleName, adminRole.Id);
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }

            }
            catch (Exception)
            {
                throw new Exception("Role update failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }
        public  List<AdminRole> GetAllRole ()
        {
            List<AdminRole>adminRoles=new List<AdminRole>();
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("Select *  from tbAdminRole");
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    AdminRole adminRole=new AdminRole();
                    adminRole.Id =Convert.ToInt32(reader[0]);
                    adminRole.RoleName = Convert.ToString(reader[1]);
                    adminRoles.Add(adminRole);
                }

            }
            catch (Exception)
            {
                throw new Exception("Role update failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return adminRoles;
        }
    }
}